/*
  包含多个由action触发去直接更新状态的方法的对象
 */
import {ADD_TODO, DELETE_TODO, SELECT_ALL_TODOS, DELETE_COMPLETE_TODOS,REQ_TODOS} from "./mutation-types";

export default {
  // 对象里面的key默认类型是字符串，如果我们想使用变量的值作为key，需要使用[]
  [ADD_TODO](state, {item}) {
    state.todos.unshift(item);
  },
  [DELETE_TODO](state, {index}) {
    state.todos.splice(index, 1);
  },
  [SELECT_ALL_TODOS](state, {value}) {
    state.todos.forEach(todo => todo.complete = value);
    /*state.todos = state.todos.map(todo => {
      return {
        title: todo.title,
        complete: value
      }
    })*/
  },
  [DELETE_COMPLETE_TODOS](state) {
    state.todos = state.todos.filter(todo => !todo.complete)
  },
  [REQ_TODOS](state, {todos}) {
    state.todos = todos;
  }
}
